package com.hotec.PROJECT_NAME.modules.sys.controller;

import com.hotec.PROJECT_NAME.modules.sys.entity.SysAttachment;
import com.hotec.PROJECT_NAME.component.basic.BaseController;
import com.hotec.PROJECT_NAME.component.basic.JsonContent;
import com.hotec.PROJECT_NAME.component.exception.exc.BizException;
import com.hotec.PROJECT_NAME.modules.sys.service.SysAttachmentService;
import com.google.common.collect.Maps;
import com.hotec.PROJECT_NAME.utils.FileService;
import com.hotec.PROJECT_NAME.utils.UtilsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.util.List;
import java.util.Map;

/**
 * @author llb
 * @date
 */
@RestController
@RequestMapping("sys/attachment")
public class SysAttachmentController extends BaseController {
    @Autowired
    private SysAttachmentService sysAttachmentService;

    public static final String UPLOAD_FILE_PARAM = "file";

    @PostMapping("upload")
    public JsonContent upload(MultipartRequest multipartRequest) {
        //上传文件的原始名称
        MultipartFile multipartFile = multipartRequest.getFile(UPLOAD_FILE_PARAM);
//        String fileName = DateUtils.formatDate(new Date(), DateUtils.PATTERN) + "/" + multipartFile.getOriginalFilename();
        String downloadURL = null;
        try {
//            downloadURL = AliOSSUtil.upload(multipartFile);
            downloadURL = FileService.uploadFile("unionpay", multipartFile);
        } catch (Exception e) {
            UtilsLog.log(this.getClass(), e);
            throw new BizException("上传失败");

        }
        Map result = Maps.newHashMap();
        result.put("attachment", downloadURL);
        //返回文件名称
        result.put("oldname", multipartFile.getOriginalFilename().replaceAll("[+ ]", "").trim());
        //文件大小
        result.put("filesize", multipartFile.getSize());
        return JsonContent.success(result);
    }

    /**
     * 删除附件
     *
     * @param sysAttachmentList
     * @return
     */
    @PostMapping("remove")
    public JsonContent remove(@RequestBody List<SysAttachment> sysAttachmentList) {
        sysAttachmentService.remove(sysAttachmentList);
        return JsonContent.success();
    }
}
